<?php

class Index_ModAction extends C_Action  
{
	private $user_E = null;
	private $site_E = null;
	private $mod_E = null;
	private $RootOper = 0;
	private $is_login = 0;
	public function init()
	{
		$this->user_E = Zend_Registry::get("user_E");
	
		
		$this->site_E = Zend_Registry::get("site_E");
		$this->mod_E = Zend_Registry::get("mod_E");
		$this->RootOper = Zend_Registry::get('RootOper');
		$this->is_login = Zend_Registry::get('is_login');
		
	}
	private function checkRootOper()
	{
		
		if($this->RootOper == 0)
		{	
			$error_url = $this->getUrl("index","error","error");
			$this->RedirectJump($error_url);
		}
	}
	private function checkLogin()
	{
		
		if($this->is_login == 0)
		{	
			$error_url = $this->getUrl("index","auth","token");
			$this->RedirectJump($error_url);
		}
	}
	public function doBulletin()
	{ 
		$this->checkRootOper();
		$update = $this->_request->get("update");
	
	
		
		//end检查站点信息 
		
		//start输出站点信息到模板
		
		$bulletin_E = Bulletins::getByModId($this->mod_E->id);
		if(empty($bulletin_E))
		{
			$content = '';
		}
		else
		{
			$content = $bulletin_E->content;
		}
		
		
		if($update)
		{
			$content = $this->_request->get("text");
			if(empty($bulletin_E))
			{
				$row['modid'] = $this->mod_E->id;
				$row['content'] = $content;
				Bulletins::insertEntity($row);
			}
			else 
			{
				$bulletin_E->content = $content;
			}
			$url = $this->getUrl("index","site","index",array("siteid"=>$this->site_E->id,"roomid"=>$this->mod_E->roomid));
			$this->_response->setRedirect($url);
		}
		$this->_response->content = $content;
		
	}
	
	public function doForum()
	{ 
		$update = $this->_request->get("update");
		$error = $this->_request->get("error");
		$this->checkLogin();
		//end检查站点信息 
		
		
	
		$this->_response->error = $error;
		if($update)
		{
			
			$title = $this->_request->get("title");
			$content = $this->_request->get("text");
			if(empty($title))
			{
				$error = "标题不能为空!";
				$error_url = $this->getUrl("index", "mod", "forum", array("siteid"=>$this->site_E->id,'modid'=>$this->mod_E->id),
											array("error"=>$error)
				);
				$this->RedirectJump($error_url);
			}
			if(empty($content))
			{
				$error = "内容不能为空!";
				$error_url = $this->getUrl("index","mod","forum",array("siteid"=>$this->site_E->id,'modid'=>$this->mod_E->id),
											array("error"=>$error)
				);
				$this->RedirectJump($error_url);
			}
			$row['modid'] = $this->mod_E->id;
			$row['userid'] = $this->user_E->id;
			$row['title'] = $title;
			$row['content'] = $content;
			Forums::insertEntity($row);
		
			$url = $this->getUrl("index","site","index",array("siteid"=>$this->site_E->id,"roomid"=>$this->mod_E->roomid));
			$this->_response->setRedirect($url);
		}
		
	
	}
	public function doModifyforum()
	{
		$this->checkRootOper();
		$forumid = $this->_request->get("forumid");
		if(empty($forumid))
		{
			$error_info = "出错";
			$jumpurl = $this->_response->getUrl('index', 'error','error',array('error_info'=>$error_info));
			$this->_response->RedirectJump($jumpurl);
		}
		$forum_E = Forums::getById($forumid);
		if(empty($forum_E))
		{
			$error_info = "出错";
			$jumpurl = $this->_response->getUrl('index', 'error','error',array('error_info'=>$error_info));
			$this->_response->RedirectJump($jumpurl);
		}
		$this->_response->forum_E = $forum_E;
		if($forum_E->userid != $this->user_E->id)
		{
			$error_info = "出错";
			$jumpurl = $this->_response->getUrl('index', 'error','error',array('error_info'=>$error_info));
			$this->_response->RedirectJump($jumpurl);
		}
		
		$update = $this->_request->get("update");
		
		$this->_response->error = $error;
		if($update)
		{
			
			$title = $this->_request->get("title");
			$content = $this->_request->get("text");
			if(empty($title))
			{
				$error = "标题不能为空!";
				$error_url = $this->getUrl("index", "mod", "modifyforum", array("siteid"=>$this->site_E->id,'modid'=>$this->mod_E->id),
											array("error"=>$error)
				);
				$this->RedirectJump($error_url);
			}
			if(empty($content))
			{
				$error = "内容不能为空!";
				$error_url = $this->getUrl("index","mod","modifyforum",array("siteid"=>$this->site_E->id,'modid'=>$this->mod_E->id),
											array("error"=>$error)
				);
				$this->RedirectJump($error_url);
			}
			$forum_E->title = $title;
			$forum_E->content = $content; 
		
			$url = $this->getUrl("index","site","index",array("siteid"=>$this->site_E->id,"roomid"=>$this->mod_E->roomid));
			$this->_response->setRedirect($url);
		}
		
	}
	public function DoRemoveforum()
	{
		$this->checkRootOper();
		$forumid = $this->_request->get("forumid");
		if(empty($forumid))
		{
			$data['error'] = "出错";
			echo json_encode($data);
			exit;
		}
		$forum_E = Forums::getById($forumid);
		if(empty($forum_E))
		{
			$data['error'] = "出错";
			echo json_encode($data);
			exit;
		}
		$this->_response->forum_E = $forum_E;
		if($forum_E->userid != $this->user_E->id)
		{
			$data['error'] = "出错";
			echo json_encode($data);
			exit;
		}
		Comments::deleteByforumid($forumid);
		$forum_E->delete();
		$data['error'] = 0;
		echo json_encode($data);
		$UnitOfWork = E_UnitOfWork::getWork();
		$UnitOfWork->commit();	
		exit;
	}
	public function doComment()
	{
	
		
		$forumid = $this->_request->get("forumid");
		
		
		if(empty($forumid))
		{
			$error_info = "出错";
			$jumpurl = $this->_response->getUrl('index', 'error','error',array('error_info'=>$error_info));
			$this->_response->RedirectJump($jumpurl);
		}
		$forum_E = Forums::getById($forumid);
		if(empty($forum_E))
		{
			$error_info = "出错";
			$jumpurl = $this->_response->getUrl('index', 'error','error',array('error_info'=>$error_info));
			$this->_response->RedirectJump($jumpurl);
		}
		
		
		
		
		$this->_response->forum_E = $forum_E;
		
		
		
		
		
		
		
		
		$comment_ES = Comments::getRoot($forum_E->id);
		$this->_response->comment_ES = $comment_ES;
		
		
		$update = $this->_request->get("update");
		if($update)
		{
			$this->checkLogin();
			$fid = $this->_request->get("fid");
			$comment = $this->_request->get("comment");
			if(empty($comment))
			{
				$url = $this->getUrl("index","mod","comment",array("siteid"=>$this->site_E->id,"modid"=>$this->mod_E->id,"forumid"=>$forum_E->id));
				$this->_response->RedirectJump($url);
			}
			if(!empty($fid))
			{
				$fcomment_E =  Comments::getById($fid);
				$fcomment_E->end = 0;
				$row['fid'] = $fcomment_E->id;
				$row['forumid'] = $forum_E->id;
				$row['userid'] = $this->user_E->id;
				$row['modid'] = $this->mod_E->id;
				$row['comment'] = $comment;
				$row['end'] = 1;
				$row['level'] = $fcomment_E->level + 1;
				$fcomment_E = Comments::insertEntity($row);
				
			}
			else if($fid == 0)
			{
				$row['forumid'] = $forum_E->id;
				$row['userid'] = $this->user_E->id;
				$row['modid'] = $this->mod_E->id;
				$row['comment'] = $comment;
				$row['end'] = 1;
				$fcomment_E = Comments::insertEntity($row);
			}
			$url = $this->getUrl("index","mod","comment",array("siteid"=>$this->site_E->id,"modid"=>$this->mod_E->id,"forumid"=>$forum_E->id));
			$this->_response->setRedirect($url);
		}
	}
	public function doPhoto()
	{
		$this->checkRootOper();
		$this->_response->phpsessid = $_COOKIE['PHPSESSID'];
	}
	public function doAllphoto()
	{
		
	}
	public function doAddphotos()
	{
		
		$this->checkRootOper();
		$image_real_path = get_image_path();
		$image_path = get_image_httppath();
		$pic_return = T_Picupload::save_pic('file', '/site/photo', '' ,array(100,100));
		if($pic_return['error'] < 0)
		{
				$data['error'] = "21";
				echo json_encode($data);
				exit;
		}
		
		$image_url = '/site/photo/'.$pic_return['newname'];
		
		$row['pic'] = $image_url;
		$row['userid'] = $this->user_E->id;
		$row['sign'] = 3;
		
		
		$pic_E = Pics::insertEntity($row);
		$UnitOfWork = E_UnitOfWork::getWork();
		$UnitOfWork->commit();	
		
		
		$image_absolute_path = $image_real_path.'/site/photo/'.$pic_return['newname'];
		new T_Resizeimage($image_absolute_path , 600,600, 0);
		new T_Resizeimage($image_absolute_path , 170,170, 0);
		new T_Resizeimage($image_absolute_path , 100,100, 1);
		$data['error']=0;
		$data['id'] = $pic_E->id;
		echo json_encode($data);
		exit;
	}
	public function doCompletephotos()
	{
		$this->checkRootOper();
		$update = $this->_request->get("update");
		if($update)
		{
			$descript_arr = $this->_request->get("descript");
			$photoid_arr = $this->_request->get("photoid");
			if(!empty($photoid_arr))
			foreach($photoid_arr as $k => $v)
			{
					if(!empty($k))
					{
						$pic_E = Pics::getById($v);
						if(!empty($pic_E) && $pic_E->userid == $this->user_E->id)
						{
							$row['photo'] = $pic_E->pic;
							$row['modid'] = $this->mod_E->id;
							$row['descript'] = $descript_arr[$v];
							$row['userid'] = $this->user_E->id;
							Photos::insertEntity($row);
							$row = array();
						}
					}	
			}
			
			$url = $this->getUrl("index","site","index",array("siteid"=>$this->site_E->id,"roomid"=>$this->mod_E->roomid));
			$this->_response->setRedirect($url);
			
		}
		else 
		{
		
			$pid = $this->_request->get("pid");
			if(empty($pid))
			{
				$error_url = $this->getUrl("index","error","error");
				$this->RedirectJump($error_url);
			}
			
			$pid_arr = explode("|", $pid);
			foreach ($pid_arr as $k => $v)
			{
				if($k==0){continue;}
				if(!empty($k))
				{
					$pic_E = Pics::getById($v);
					if(!empty($pic_E) && $pic_E->userid == $this->user_E->id)
					{
						$pic_list_E[] = $pic_E;
					}
				}	
			}
			
			$this->_response->pic_list_E = $pic_list_E;
		}
	}
	public function doModifyphoto()
	{
		$this->checkRootOper();
		$photoid = $this->_request->get('photoid');
		
		if(empty($photoid))
		{
			$error_url = $this->getUrl("index","error","error");
			$this->RedirectJump($error_url);
		}
		$photo_E = Photos::getById($photoid);
		if(empty($photo_E))
		{
			$error_url = $this->getUrl("index","error","error");
			$this->RedirectJump($error_url);
		}
		if($photo_E->userid != $this->user_E->id)
		{
			$error_url = $this->getUrl("index","error","error");
			$this->RedirectJump($error_url);
		}
		$update = $this->_request->get("update");
		if($update)
		{
			$descript = $this->_request->get("descript");
			$photo_E->descript = $descript;
			$url = $this->getUrl("index","mod","detailphoto",array("siteid"=>$this->site_E->id,"modid"=>$this->mod_E->id,"photoid"=>$photo_E->id));
			$this->_response->setRedirect($url);
		}
		
		$this->_response->photo_E = $photo_E;
	}
	public function doDetailphoto()
	{
		$photoid = $this->_request->get('photoid');
		
		if(empty($photoid))
		{
			$error_url = $this->getUrl("index","error","error");
			$this->RedirectJump($error_url);
		}
		$photo_E = Photos::getById($photoid);
		if(empty($photo_E))
		{
			$error_url = $this->getUrl("index","error","error");
			$this->RedirectJump($error_url);
		}
		
		$photo_AS_A = Photos::getListArrayByModid($this->mod_E->id);
	
		foreach($photo_AS_A as $k => $v)
		{
			$photo_AS[] = $v['id'];
		}
		$num = count($photo_AS);
		if(in_array((int)$photoid, $photo_AS))
		{
			$current_num = array_search($photoid, $photo_AS); 
			if($current_num == 0)
			{
				if($num == 1)
				{
					$next_photoid = $photo_AS[0];
					$pre_photoid = $photo_AS[$num-1];
				}
				else
				{
					$next_photoid = $photo_AS[1];
					$pre_photoid = $photo_AS[$num-1];
				}
			}
			else if($current_num + 1 == $num)
			{
				$next_photoid = $photo_AS[0];
				$pre_photoid = $photo_AS[$num-2];
			}
			else 
			{
				$next_photoid = $photo_AS[$current_num+1];
				$pre_photoid = $photo_AS[$current_num-1];
			}
		}
		
		$this->_response->photo_E = $photo_E;
		$this->_response->next_photoid = $next_photoid;
		$this->_response->pre_photoid = $pre_photoid;
		$this->_response->photo_num = $num;
		$this->_response->photo_current_num = $current_num+1;
		
	}
	public function doRemovephoto()
	{
		$this->checkRootOper();
		$photoid = $this->_request->get("photoid");
		if(empty($photoid))
		{
			$data['error'] = "出错";
			echo json_encode($data);
			exit;
		}
		$photo_E = Photos::getById($photoid);
		if(empty($photo_E))
		{
			$data['error'] = "出错";
			echo json_encode($data);
			exit;
		}
		if($photo_E->userid != $this->user_E->id)
		{
			$data['error'] = "出错";
			echo json_encode($data);
			exit;
		}
		$photo_E->delete();
		$data['error'] = 0;
		echo json_encode($data);
		$UnitOfWork = E_UnitOfWork::getWork();
		$UnitOfWork->commit();	
		exit;
	}
	public function doMessage()
	{
		$this->checkLogin();
		$message = $this->_request->get("text");
		if(empty($message))
		{
			$error_url = $this->getUrl("index","error","error");
			$this->RedirectJump($error_url);
		}
	
		$row['modid'] = $this->mod_E->id;
		$row['message'] = $message;
		$row['userid'] = $this->user_E->id;
		Messages::insertEntity($row);
		$url = $this->getUrl("index","site","index",array("siteid"=>$this->site_E->id,"roomid"=>$this->mod_E->roomid));
		$this->_response->setRedirect($url);
	}
}

?>
